package cn.com.qingz.modules.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.modules.user.mapper.OrganizationMapper;
import cn.com.qingz.modules.user.model.City;
import cn.com.qingz.modules.user.model.Organization;
import cn.com.qingz.modules.user.model.SchoolVO;
import cn.com.qingz.modules.user.service.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationMapper orgMapper;

    public List<Organization> getChildren(Long pid) {
        return orgMapper.getChildrenByPid(pid);
    }

    public List<City> listCity() {
        return orgMapper.listCity();
    }

    public void doAddSchool(SchoolVO vo) {
        Long cityId = vo.getCityId();

        int x = orgMapper.checkSchool(vo.getCityName() + "-" + vo.getSchoolName());
        if (x > 0) {
            throw new DicmfException(SystemConstantes.ERROR, "该学校已经存在");
        }

        //添加学校
        Organization school = new Organization();
        school.setPid(cityId);
        school.setLevel(2);
        school.setOrgName(vo.getSchoolName());
        school.setParent_path_id(cityId + "");
        school.setFull_path_name(vo.getCityName() + "-" + vo.getSchoolName());
        orgMapper.addOrg(school);

        Long schoolId = school.getId();

        createGrade(vo, cityId, school, schoolId, "幼儿园小班");
        createGrade(vo, cityId, school, schoolId, "幼儿园中班");
        createGrade(vo, cityId, school, schoolId, "幼儿园大班");
        createGrade(vo, cityId, school, schoolId, "一年级");
        createGrade(vo, cityId, school, schoolId, "二年级");
        createGrade(vo, cityId, school, schoolId, "三年级");
        createGrade(vo, cityId, school, schoolId, "四年级");
        createGrade(vo, cityId, school, schoolId, "五年级");
        createGrade(vo, cityId, school, schoolId, "六年级");

    }

    private void createGrade(SchoolVO vo, Long cityId, Organization school, Long schoolId, String gradeName) {

        Integer classNumber = 0; //班级个数

        if ("幼儿园小班".equals(gradeName)) {
            classNumber = vo.getCh1();
        }
        if ("幼儿园中班".equals(gradeName)) {
            classNumber = vo.getCh2();
        }
        if ("幼儿园大班".equals(gradeName)) {
            classNumber = vo.getCh3();
        }
        if ("一年级".equals(gradeName)) {
            classNumber = vo.getGrade1();
        }
        if ("二年级".equals(gradeName)) {
            classNumber = vo.getGrade2();
        }
        if ("三年级".equals(gradeName)) {
            classNumber = vo.getGrade3();
        }
        if ("四年级".equals(gradeName)) {
            classNumber = vo.getGrade4();
        }
        if ("五年级".equals(gradeName)) {
            classNumber = vo.getGrade5();
        }
        if ("六年级".equals(gradeName)) {
            classNumber = vo.getGrade6();
        }

        if (classNumber == null || classNumber == 0) {
            return;
        }

        //添加年级
        Organization grade = new Organization();
        grade.setLevel(3);
        grade.setOrgName(gradeName);
        grade.setParent_path_id(cityId + "-" + schoolId);
        grade.setFull_path_name(school.getFull_path_name() + "-" + gradeName);
        grade.setPid(schoolId);

        orgMapper.addOrg(grade);
        Long gradeId = grade.getId();

        //添加班级
        for (int i = 1; i <= classNumber; i++) {
            Organization class_n = new Organization();
            class_n.setLevel(4);
            String number = format(i + "");
            class_n.setOrgName(number + "班");
            class_n.setParent_path_id(cityId + "-" + schoolId + "-" + gradeId);
            class_n.setFull_path_name(grade.getFull_path_name() + "-" + class_n.getOrgName());
            class_n.setPid(gradeId);
            orgMapper.addOrg(class_n);
        }
    }

    public PageList<SchoolVO> querySchool(int pageNo, int limit) {
        PageList<SchoolVO> list = orgMapper.querySchool(new PageBounds(pageNo, limit));
        return list;
    }

    /**
     * 19以内的数字转文字
     * @param text
     * @return
     */
    public static String format(String text) {
        for (int i = 0; i < 10; i++) {
            text = text.replace((char) ('1' + i),
                    "一二三四五六七八九十".charAt(i));
        }
        if (text.length() == 2) {
            text = text.replace("一", "十");
        }
        return text;
    }

}
